﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Helpers.Managers
{
    /// <summary>
    /// Handles a set of <typeparamref name="Managers.IManager"/> objects together.
    /// </summary>
    public class ManagersHandling : IManager, IDisposable
    {
        #region Fields

        /// <summary>
        /// A read only collection of <typeparamref name="Managers.IManager"/> objects to be handled together.
        /// </summary>
        protected ReadOnlyCollection<IManager> managers;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <typeparamref name="Managers.ManagersHandling"/> class.
        /// </summary>
        /// <param name="managersList">A list of <typeparamref name="Managers.IManager"/> objects to be handle
        /// together.</param>
        public ManagersHandling(IEnumerable<IManager> managers)
        {
            this.managers = new ReadOnlyCollection<IManager>(managers.ToList());
        }

        #endregion

        #region Methods

        #region IManager Members

        /// <summary>
        /// Releases all resources used by the current instance of <typeparamref name="Managers.ManagerBase"/> and all its managers.
        /// </summary>
        public virtual void Dispose()
        {
            foreach (IManager iManager in this.managers)
                iManager.Dispose();
        }
        /// <summary>
        /// Start all the managers.
        /// </summary>
        public virtual void Start()
        {
            foreach (IManager iManager in this.managers)
                iManager.Start();
        }
        /// <summary>
        /// Stop all the managers.
        /// </summary>
        public virtual void Stop()
        {
            foreach (IManager iManager in this.managers)
                iManager.Stop();
        }

        #endregion

        #endregion
    }
}
